import {Service, utils} from "@codefree/badger/service";

import {LoginEntity, SignUpEnyity} from '../viewmodel/auth_entities'
import {authUserEntity} from '../model/authuser.db'

export default class AuthService extends Service {
    /**
     * 登录
     * @param loginEntity
     * @returns {Promise.<object>} - AuthUser
     */
    async login(loginEntity: LoginEntity): authUserEntity {
        let authUser = await this.ctx.model.AuthUser.findOne({
            where: {
                username: loginEntity.username,
                password: utils.formatPassword(loginEntity.password)
            }
        });
        return authUser;
    }

    /**
     * 注册
     * @param signUpEntity
     * @returns {Promise.<boolean>} - 新用户id
     */
    async signUp(signUpEntity: SignUpEnyity): Boolean {
        let authUser = await this.ctx.model.AuthUser.create({
            username: signUpEntity.username,
            password: utils.formatPassword(signUpEntity.password)
        });
        return authUser != null;
    }


}